package com.fiidee.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.fiidee.blog.common.ERRORCODE;
import com.fiidee.blog.service.UserService;
import com.fiidee.blog.util.ReturnJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * Created by 瑱彬 on 2015/5/26.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public JSONObject doLogin(String loginCode, String password, HttpServletRequest request, HttpServletResponse response) {
        int code = userService.doLogin(loginCode, password, response);
        return ReturnJsonUtil.returnMsg(code);
    }

    @RequestMapping("/logout")
    @ResponseBody
    public JSONObject doLogout(HttpServletResponse response) {
        int code = userService.doLogout(response);
        return ReturnJsonUtil.returnMsg(code);
    }


    @RequestMapping("/find")
    @ResponseBody
    public JSONObject findUser(String name){
        return ReturnJsonUtil.successRows(userService.findUser(name));
    }

    /**
     * 修改秘密
     */
    @RequestMapping("/modifyPassword")
    @ResponseBody
    public JSONObject doModifyPassword(@CookieValue(value = "userId", required = true, defaultValue = "") String userId, String oldPassword, String newPassword) {
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        int code = userService.doModifyPassword(id, oldPassword, newPassword);
        return ReturnJsonUtil.returnMsg(code);
    }

    /**
     * 修改邮件
     */
    @RequestMapping("/modifyEmail")
    @ResponseBody
    public JSONObject doModifyEmail(@CookieValue(value = "userId", required = true, defaultValue = "") String userId, String password, String newEmail) {
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        int code = userService.doModifyEmail(id,password,newEmail);
        return ReturnJsonUtil.returnMsg(code);
    }

    /**
     * 修改电话号码
     */
    @RequestMapping("/modifyMobile")
    @ResponseBody
    public JSONObject doModifyMobile(@CookieValue(value = "userId", required = true, defaultValue = "") String userId, String password, String newMobile){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        int code = userService.doModifyMobile(id, password, newMobile);
        return ReturnJsonUtil.returnMsg(code);

    }

    /**
     * 修改头像
     */
    @RequestMapping("/modifyAvatar")
    @ResponseBody
    public JSONObject doModifyAvatar(@CookieValue(value = "userId",required = true,defaultValue = "")String userId,
                                     String imgUrl,
                                     HttpServletResponse response){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        int code = userService.doModifyAvatar(id,imgUrl,response);
        return ReturnJsonUtil.returnMsg(code);
    }

    /**
     * 修改用户信息
     */
    @RequestMapping("/modifyInfo")
    @ResponseBody
    public JSONObject doModifyInfo(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                                   String name,
                                   String alias,
                                   String realname,
                                   String role,
                                   String email,
                                   String number,
                                   String summary){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        int code = userService.doModifyInfo(id, name,alias,realname,role,email,number, summary);
        return ReturnJsonUtil.returnMsg(code);
    }
}
